
window.onload=function(){
	//得到当前页面所有的grid对象
	var grids=$("table[class='easyui-datagrid']");
	for(var i=0;i<grids.length;i++){
		//只处理有值的
		if("undefined" != typeof $(grids[i]).attr("id")){	
			//inital_grid($(grids[i]).attr("id"));  加上这句会查询两次
			inital_grid_pagination($(grids[i]).attr("id"));
		}
		
	}
	
}

/**
 * 初始化grid的分页栏
 * @param tablename  grid id
 * @return
 */
function inital_grid_pagination(tablename){

	//begin
	$('#'+tablename).datagrid('getPager').pagination({  
	    beforePageText: '第',//页数文本框前显示的汉字   
	    afterPageText: '页    共 {pages} 页',   
	    displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录 Power By <font style=\"color:red;\"><b>JeasyTag V0.1(beta)</b></font>',   
	    onChangePageSize:function(pageSize){
				//当用户修改每页显示记录数时触发
	    	
				
	    },	    
	    onSelectPage:function(pageNumber, pageSize){//跳转页面前调用
	    	$('#'+tablename).datagrid('options').pageNumber=pageNumber;
	    	$('#'+tablename).datagrid('options').pageSize=pageSize;	    	
	    	$('#'+tablename).datagrid('reload',$('#'+tablename).datagrid('options').queryParams);
		}
	});
	//end

}


/**
 * 根据列标题返回字段名
 * @param ftitle
 * @param tablename
 * @return
 */
function getFieldsName(ftitle,tablename){	
	var filedsname="";
	var fields = $('#'+tablename).datagrid('options').columns;
		for(var i=0; i<fields[0].length; i++){
			if(fields[0][i].title==ftitle){
			filedsname=fields[0][i].field;
			break;
			}
		}
		return filedsname;
	}
/**
 * 创建grid 列的右键菜单
 * @param tablename
 * @return
 */
function createColumnMenu(tablename){
	var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
	var fields = $('#'+tablename).datagrid('options').columns;
	for(var i=0; i<fields[0].length; i++){
		if(!fields[0][i].checkbox){
			$('<div iconCls="icon-ok"/>').html(fields[0][i].title).appendTo(tmenu);
		}		
		
	}
	tmenu.menu({			
		onClick: function(item){
			if (item.iconCls=='icon-ok'){
				$('#'+tablename).datagrid('hideColumn', getFieldsName(item.text,tablename));
				tmenu.menu('setIcon', {
					target: item.target,
					iconCls: 'icon-empty'
				});
			} else {
				$('#'+tablename).datagrid('showColumn', getFieldsName(item.text,tablename));
				tmenu.menu('setIcon', {
					target: item.target,
					iconCls: 'icon-ok'
				});
			}
		}
	});
}

/**
 * 初始化grid
 * @param tablename
 * @return
 */
function inital_grid(tablename){
	$('#'+tablename).datagrid({
		onHeaderContextMenu: function(e, field){//列的右键
			e.preventDefault();
			if (!$('#tmenu').length){
				createColumnMenu(tablename);
			}
			$('#tmenu').menu('show', {
				left:e.pageX,
				top:e.pageY
			});
		}
	});


}

/**
 * 得到grid查询参数信息
 * [{field:'',editor:''}]
 * @param grid_id
 * @return
 */
function getQueryArgsInfo(grid_id){		
	var fields = $('#'+grid_id).datagrid('options').columns;
	var frozen_field=$('#'+grid_id).datagrid('options').frozenColumns;
	var field_data=[];
	var finnally_data=[];//最张处理结果
	//普通列
	for(var i=0;i<fields.length;i++){
		for(var j=0;j<fields[i].length;j++){
			if(!fields[i][j].checkbox && "undefined" != typeof fields[i][j].editor){
			field_data.push(fields[i][j]);			
			}
			
		}
	}
	//固定冻结列
	for(var i=0;i<frozen_field.length;i++){
		for(var j=0;j<frozen_field[i].length;j++){
			if(!frozen_field[i][j].checkbox && "undefined" != typeof frozen_field[i][j].editor){
			field_data.push(frozen_field[i][j]);			
			}
			
		}
	}	
	for(var i=0;i<field_data.length;i++){		
		var temp = new Object();
		temp.filed=field_data[i].field;
		temp.editor=field_data[i].editor.type;
		finnally_data.push(temp);
		
	}	
	return finnally_data;
}